API Automation with RequestsLibrary
RequestsLibrary enables REST API automation in Robot Framework by providing high-level keywords on top of Pythonβs Requests library.
It is commonly used for API testing, integration testing, and backend validation in automation frameworks.
What is RequestsLibrary?β
RequestsLibrary:
- Wraps Python Requests
- Provides session-based API testing
- Integrates cleanly with Robot Framework
Used for:
- REST API automation
- Backend validation
- API + UI hybrid testing
Creating API Sessionsβ
Sessions allow reuse of base URLs and headers.
Create Session mysession https://api.example.com
Benefits:
- Cleaner test cases
- Reusable configuration
- Better performance
HTTP Methodsβ
GET Requestβ
${response}= GET On Session mysession /users
POST Requestβ
${response}= POST On Session mysession /users json=${payload}
PUT / PATCH / DELETEβ
Used for update and delete operations.
Headers and Payloadsβ
Headers:
&{headers}= Create Dictionary Content-Type=application/json
Payloads:
- JSON dictionaries
- External JSON files
- Dynamic data
Response Validationβ
Common validations:
- Status code
- Response body
- Headers
Example:
Should Be Equal As Integers ${response.status_code} 200
JSON Parsingβ
Use built-in and Collections library to parse JSON responses.
Common use cases:
- Field-level validation
- Extracting IDs for next calls
API Test Design Best Practicesβ
- Keep API logic in keywords
- Use sessions consistently
- Validate business behavior
- Avoid UI-style waits
Common Mistakes ββ
- Hardcoding URLs
- No session usage
- Over-validating response structure
- Mixing UI and API logic
Key Takeawaysβ
- RequestsLibrary enables API automation
- Session-based execution is recommended
- Clean validations improve reliability
- API tests are CI-friendly